package com.xinxing.learning.batch.console;

import com.xinxing.learning.batch.common.LogConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

@Slf4j
public class StringReader implements ItemReader<String> {
    private String[] messages = {"aaa1", "aaa2", "aaa3", "aaa4"};
    private int count = 0;

    @Override
    public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        if (count < messages.length) {
            String message = messages[count++];
            log.info(LogConstants.LOG_TAG + "read data:" + message);
            return message;
        } else {
            log.info(LogConstants.LOG_TAG + "read data end.");
            count = 0;
        }
        return null;
    }
}
